package com.example.robertluozizhao.framecollectdemo.activity.test.rongcloud.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.SectionIndexer;
import android.widget.TextView;

import com.example.robertluozizhao.framecollectdemo.R;
import com.example.robertluozizhao.framecollectdemo.activity.test.rongcloud.bean.Friend;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by AMing on 16/1/14.
 * Company RongCloud
 */
public class FriendAdapter extends BaseAdapter implements SectionIndexer {

    private Context context;

    private List<Friend> list = new ArrayList();
    public Map<Friend,Integer> mQunzhu = new HashMap<>();
    public LinkedHashMap<Friend,Integer> mManager = new LinkedHashMap<>();
    public LinkedHashMap<Friend,Integer> mMember = new LinkedHashMap<>();
    public Map<String,Integer> letterIndex = new HashMap<>(); //letter index
    public Map<String,Integer> letterCount = new HashMap<>(); //letter count




    public FriendAdapter(Context context, List<Friend> list) {
        this.context = context;
        this.list = list;
    }


    /**
     * 传入新的数据 刷新UI的方法
     */
    public void updateListView(List<Friend> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        final Friend mContent = list.get(position);
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.friend_item, null);
            viewHolder.tvTitle = (TextView) convertView.findViewById(R.id.friendname);
            viewHolder.tvLetter = (TextView) convertView.findViewById(R.id.catalog);
            viewHolder.mImageView = (ImageView) convertView.findViewById(R.id.frienduri);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        if (mContent.getType()==2) {
            viewHolder.tvLetter.setVisibility(View.VISIBLE);
            viewHolder.tvLetter.setText(String.format("管理员(%s)人",mManager.size()+1));
        }else if (mContent.getType()==1){
            viewHolder.tvLetter.setVisibility(View.GONE);
        }else{
            //根据position获取分类的首字母的Char ascii值
            int section = getSectionForPosition(position);
            //如果当前位置等于该分类首字母的Char的位置 ，则认为是第一次出现
            if (position == getPositionForSection(section)) {
                viewHolder.tvLetter.setVisibility(View.VISIBLE);
                viewHolder.tvLetter.setText(mContent.getLetters()+String.format("共有成员(%s)人",letterCount.get(mContent.getLetters())));
            } else {
                viewHolder.tvLetter.setVisibility(View.GONE);
            }
        }
        viewHolder.tvTitle.setText(this.list.get(position).getName());
        return convertView;
    }

    @Override
    public Object[] getSections() {
        return new Object[0];
    }

    @Override
    public int getPositionForSection(int sectionIndex) {
        for (int i = 0; i < getCount(); i++) {
            if (list.get(i).getType()==0) {
                String sortStr = list.get(i).getLetters();
                char firstChar = sortStr.toUpperCase().charAt(0);
                if (firstChar == sectionIndex) {
                    return i;
                }
            }
        }
        return -1;
    }

    @Override
    public int getSectionForPosition(int position) {
        return list.get(position).getLetters().charAt(0);
    }

    public void setList(List<Friend> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    final static class ViewHolder {
        /**
         * 首字母
         */
        TextView tvLetter;
        /**
         * 昵称
         */
        TextView tvTitle;
        /**
         * 头像
         */
        ImageView mImageView;
        /**
         * userid
         */
//        TextView tvUserId;
    }
}
